package vumt.radiocountries;

import java.io.InputStream;
import java.util.ArrayList;

import vumt.radiocountries.dialog.DialogNoticeNetwork;
import vumt.radiocountries.object.ChannelObject;
import vumt.radiocountries.util.ChannelParser;
import vumt.radiocountries.util.Global;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

/*
 * @ Màn hình Splash khi bắt đầu vào app
 * @ Có xử lý việc lấy data XML từ server
 * 
 * */

public class SplashActivity extends Activity {
	ChannelParser parser;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash_layout);
		initImageLoader(this);
		parser = new ChannelParser();

		new asyncXML().execute();
	}

	private class asyncXML extends AsyncTask<Void, Void, Void> {
		ProgressDialog dialog;

		@Override
		protected void onPreExecute() {
			Global.INDEX_ERROR_INTERNET = 0;
			dialog = ProgressDialog.show(SplashActivity.this, "",
					"Loading..Wait..", true);
			dialog.show();
			super.onPreExecute();
		}

		@Override
		protected Void doInBackground(Void... params) {
			getListChannel();
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// state normal internet
			if (Global.INDEX_ERROR_INTERNET == 0) {
				Intent i = new Intent(SplashActivity.this, MainActivity.class);
				startActivity(i);

				finish();
			}
			// state Slow internet -> error
			else if (Global.INDEX_ERROR_INTERNET == 1) {
				Intent i = new Intent(SplashActivity.this,
						DialogNoticeNetwork.class);
				startActivityForResult(i, 1);
			}

			dialog.dismiss();
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		default:
			break;
		}

		if (resultCode == 2)
			finish();
		if (resultCode == 3)
			new asyncXML().execute();
	}

	// Hàm thực hiện tải lại
	public void reload() {

	}

	public void getListChannel() {
		InputStream inputStream = parser
				.getStreamFromUrl(Global.LINK_XML_CONFIG);
		Global.listChannel = new ArrayList<ChannelObject>();
		try {
			String xml = parser.convertStreamToString(inputStream);
			Global.listChannel = parser.getChannelList(xml);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void initImageLoader(Context context) {
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				context).threadPriority(Thread.NORM_PRIORITY - 2)
				.denyCacheImageMultipleSizesInMemory()
				.discCacheFileNameGenerator(new Md5FileNameGenerator())
				.tasksProcessingOrder(QueueProcessingType.LIFO).enableLogging()
				.build();
		ImageLoader.getInstance().init(config);
	}
}
